package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.CategoryBackDTO;
import com.jyh.blog.dto.CategoryDTO;
import com.jyh.blog.entity.Category;
import com.jyh.blog.service.CategoryService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;

@Api(tags = "分类管理")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类
     * @return
     */
    @ApiOperation(value = "查询首页分类")
    @GetMapping("/categories")
    public Result<List<CategoryDTO>> list(){
        return Result.ok(categoryService.categoryList());
    }

    /**
     *查询后台分类
     * @return
     */
    @ApiOperation(value = "查询后台分类")
    @PostMapping("/admin/categories")
    public Result list(@RequestBody ConditionVO condition){
        PageResult<CategoryBackDTO> list = categoryService.categoryBackList(condition);
        return Result.ok(list);
    }

    /**
     * 添加或修改分类
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改分类")
    @PostMapping("/admin/category")
    public Result addOrUpdate(@RequestBody Category category){
        categoryService.addOrUpdate(category);
        return Result.ok();
    }

    /**
     * 删除分类
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除分类")
    @DeleteMapping("/admin/category/{id}")
    public Result deleteById(@PathVariable String id){
        categoryService.deleteCategoryById(id);
        return Result.ok();
    }


}
